﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.Binaria.Core.Web;
using Com.Binaria.SISCOB.Funcional.DTO;
using Com.Binaria.Core.Dto;

namespace Com.Binaria.SISCOB.Web.UI 
{

    public partial class ucEgreso : System.Web.UI.UserControl, IView<EgresoFuncionalDTO>
    {
        #region Miembros de IView<EgresoFuncionalDTO>

        public event ViewAddHandler OnAdd;
        public event ViewSaveHandler OnSave;

        public string Message
        {
            set { this.lblMessage.Text = value; }
        }

        public IList<EgresoFuncionalDTO> Items
        {
            set { }
        }

        public EgresoFuncionalDTO Item
        {
            set
            {
                if (value == null) return;
                //Loop por cada una de las propiedades
                ctlId.Value = value.Id.ToString();
                ctlEsManualE.Checked = value.EsManual;
                ctlEstadoE.SelectedValue = value.Estado;

                //Relacion [MD11]: DocumentoTransaccion
                if (value.DocumentoTransaccion != null)
                {
                    ctlIdKyAc11DocumentoTransaccion.Value = value.DocumentoTransaccion.Id.ToString();
                    ctlSucursalKyAc11DocumentoTransaccionE.SelectedValue = value.DocumentoTransaccion.Sucursal.ToString();
                    ctlCuentaCorrienteKyAc11DocumentoTransaccionE.SelectedValue = value.DocumentoTransaccion.CuentaCorriente.ToString();
                    ctlMontoKyAc11DocumentoTransaccionE.Text = value.DocumentoTransaccion.Monto.ToString();
                    ctlFchDocumentoTextKyAc11DocumentoTransaccionE.Text = value.DocumentoTransaccion.FchDocumentoText;
                    ctlFchUpdateTextKyAc11DocumentoTransaccionE.Text = value.DocumentoTransaccion.FchUpdateText;
                    ctlGlosaKyAc11DocumentoTransaccionE.Text = value.DocumentoTransaccion.Glosa;
                    ctlNmrDocumentoKyAc11DocumentoTransaccionE.Text = value.DocumentoTransaccion.NmrDocumento;
                    ctlUserNameKyAc11DocumentoTransaccionE.Text = value.DocumentoTransaccion.UserName;
                }
            }
            get
            {
                EgresoFuncionalDTO oItem = new EgresoFuncionalDTO();
                if (!string.IsNullOrEmpty(ctlId.Value)) oItem.Id = Convert.ToInt32(ctlId.Value);
                oItem.EsManual = ctlEsManualE.Checked;
                oItem.Estado = ctlEstadoE.SelectedValue;

                //Relacion [MD11]: DocumentoTransaccion
                oItem.DocumentoTransaccion = new DocumentoTransaccionFuncionalDTO();
                if (!string.IsNullOrEmpty(ctlIdKyAc11DocumentoTransaccion.Value)) oItem.DocumentoTransaccion.Id = Convert.ToInt32(ctlIdKyAc11DocumentoTransaccion.Value);
                oItem.DocumentoTransaccion.Sucursal = Convert.ToInt32(ctlSucursalKyAc11DocumentoTransaccionE.SelectedValue);
                oItem.DocumentoTransaccion.CuentaCorriente = Convert.ToInt32(ctlCuentaCorrienteKyAc11DocumentoTransaccionE.SelectedValue);
                if (!string.IsNullOrEmpty(ctlMontoKyAc11DocumentoTransaccionE.Text)) oItem.DocumentoTransaccion.Monto = Convert.ToInt64(ctlMontoKyAc11DocumentoTransaccionE.Text);
                if (!string.IsNullOrEmpty(ctlFchDocumentoTextKyAc11DocumentoTransaccionE.Text)) oItem.DocumentoTransaccion.FchDocumentoText = ctlFchDocumentoTextKyAc11DocumentoTransaccionE.Text;
                if (!string.IsNullOrEmpty(ctlFchUpdateTextKyAc11DocumentoTransaccionE.Text)) oItem.DocumentoTransaccion.FchUpdateText = ctlFchUpdateTextKyAc11DocumentoTransaccionE.Text;
                if (!string.IsNullOrEmpty(ctlGlosaKyAc11DocumentoTransaccionE.Text)) oItem.DocumentoTransaccion.Glosa = ctlGlosaKyAc11DocumentoTransaccionE.Text;
                if (!string.IsNullOrEmpty(ctlNmrDocumentoKyAc11DocumentoTransaccionE.Text)) oItem.DocumentoTransaccion.NmrDocumento = ctlNmrDocumentoKyAc11DocumentoTransaccionE.Text;
                if (!string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) oItem.DocumentoTransaccion.UserName = HttpContext.Current.User.Identity.Name;
                //oItem.DocumentoTransaccion.EstaActivo = ctlEstaActivoKyAc11DocumentoTransaccionE.Checked;
                return oItem;
            }
        }

        IRelacion[] _Relaciones = null;
        public IRelacion[] Relaciones
        {
            get
            {
                if (_Relaciones == null)
                {
                    _Relaciones = new RelacionItem[2];
                    _Relaciones[0] = new RelacionItem() { IdentityName = "Sucursal", Target = new string[] {"SucursalKyAc11DocumentoTransaccion"} };
                    _Relaciones[1] = new RelacionItem() { IdentityName = "CuentaCorriente", Target = new string[] {"CuentaCorrienteKyAc11DocumentoTransaccion","CuentaCorriente"} };
                }
                return _Relaciones;
            }
        }

        public void ChangeMode(TypeAction Action)
        {
            pnlFind.Visible = false;
            pnlEdit.Visible = false;
            pnlGrid.Visible = false;
            //
            switch (Action)
            {
                case TypeAction.ALL:
                case TypeAction.FND:
                    pnlFind.Visible = true;
                    pnlGrid.Visible = true;
                    break;
                case TypeAction.EDI:
                case TypeAction.ADD:
                    pnlEdit.Visible = true;
                    break;
            }
        }

        public void DataBindRelaciones(TypeAction Action)
        {
            DropDownList oCbo = null;
            if (Relaciones == null) return;
            for (int i = 0; i < Relaciones.Length; i++)
            {
               for (int t = 0; t < Relaciones[i].Target.Length; t++)
               {
                    oCbo = FindControl(string.Format("ctl{0}{1}"
                                                                , Relaciones[i].Target[t]
                                                                , (Action == TypeAction.ALL) ? "" : "E")) as DropDownList;
                    if (oCbo != null)
                    {
                        oCbo.DataSource = Relaciones[i].DataSource;
                        oCbo.DataBind();
                    }
               }
            }
            oCbo = FindControl(string.Format("ctl{0}{1}"
                                                        , "Estado"
                                                        , (Action == TypeAction.ALL) ? "" : "E")) as DropDownList;
            oCbo.DataSource = new DominioDTO[6] { new DominioDTO() { Clave = "0", Valor = " Seleccionar ..." }, new DominioDTO() { Clave = "GI", Valor = "Girado" }, new DominioDTO() { Clave = "CA", Valor = "Caducado" }, new DominioDTO() { Clave = "PA", Valor = "Pagado" }, new DominioDTO() { Clave = "ON", Valor = "No Contabilizado" }, new DominioDTO() { Clave = "OI", Valor = "Observado" } };
            oCbo.DataBind();

        }
        #endregion

        #region EVENTOS PROPIOS DEL CONTROL
        protected void btnGrabar_Click(object sender, EventArgs e)
        {
            if (OnSave != null && OnSave(false))
            {
                Page.ClientScript.RegisterStartupScript(typeof(ucEgreso), "closeBox", "if (typeof(self.parent.$.Mantto) != null) {self.parent.$.Mantto.ActionEditClose('Informaci&oacute;n registrada satisfactoriamente');}", true);
            }
        }
        #endregion
    }
}
